package controller;

import java.io.File;

import javax.servlet.http.HttpSession;

import mapper.InformMapper;
import mapper.GroupMapper;
import mapper.TaskMapper;
import mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import controller.Vo.Vo;
import entity.Inform;
import entity.Task;
import entity.User;
import service.NoteService;
import service.ServiceException;
import serviceImpl.DownloadView;

@Controller
public class OfficeController {
	@Autowired
	private NoteService noteService;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private GroupMapper groupMapper;
	@Autowired
	private InformMapper informMapper;
	@Autowired
	private TaskMapper taskMapper;
	
	@RequestMapping("/home.do")
	public String home(){
		return "login";
	}
	
	@RequestMapping("/login.do")
	public ModelAndView login(@RequestParam("username")String username,
							  @RequestParam("password")String password,HttpSession session,ModelAndView mav){
		User user = noteService.selectUserByUserName(username, password);
		if(user == null){
			mav.addObject("msg","登录失败，用户名或密码错误");
			mav.setViewName("login");
		}else{
			session.setAttribute("user", user);
			mav.addObject("user", user);
			mav.setViewName("index");
		}
		return mav;
	}
	
	@RequestMapping("/showOfficeManagePage.do")
	public ModelAndView showOfficeManagPage(ModelAndView mav){
		mav.setViewName("officeManage");
		return mav;
	}
	
	/**
	 * 组管理
	 */
	@RequestMapping("/officeManage/groupManange/showHaveGroups.do")
	public ModelAndView showHaveGroups(ModelAndView mav,HttpSession session){
		User user = (User)session.getAttribute("user");
		if(null == user){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("groups", groupMapper.selectGroupsByCreateUser(user));
		
		mav.setViewName("/groupManage/showHaveGroups");
		return mav;
	}
	
	@RequestMapping("/officeManage/groupManange/newGroup.do")
	public ModelAndView newGroup(ModelAndView mav,HttpSession session){
		User user = (User) session.getAttribute("user");
		if(null == user){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("user", user);
		mav.addObject("users", userMapper.selectAllUsers());
		
		mav.setViewName("/groupManage/newGroup");
		return mav;
	}
	
	@RequestMapping("/officeManage/groupManange/showAllUsers.do")
	public ModelAndView showAllUsers(ModelAndView mav,HttpSession session){
		User currentUser = (User) session.getAttribute("user");
		if(null == currentUser){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("user", currentUser);
		mav.addObject("users", userMapper.selectAllUsers());
		
		mav.setViewName("/groupManage/allUsers");
		return mav;
	}
	
	@RequestMapping("/officeManage/groupManange/addGroup.do")
	@ResponseBody
	public Vo addGroup(@RequestParam("groupName")String groupName,
					   @RequestParam("userIds[]")int[] userIds,HttpSession session){
		User currentUser = (User) session.getAttribute("user");
 		noteService.addGroup(groupName, userIds, currentUser.getId());
		
		return Vo.succ("添加成功");
	}
	
	@RequestMapping("/officeManage/groupManange/showGroupMember.do")
	public ModelAndView showGroupMember(@RequestParam("groupId")Integer groupId,ModelAndView mav){
		mav.addObject("groupMembers",noteService.selectCurrentGroupMembers(groupId));
		
		mav.setViewName("/groupManage/groupMemberPage");
		return mav;
	}
	
	@RequestMapping("/officeManage/groupManange/showGroupInform.do")
	public ModelAndView showGroupInform(@RequestParam("groupId")Integer groupId,ModelAndView mav,HttpSession session){
		User user = (User)session.getAttribute("user");
		if(null == user){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("currentUser", user);
		mav.addObject("userInforms", noteService.showGroupInforms(groupId));
		
		mav.setViewName("/groupManage/informs");
		return mav;
	}
	
	@RequestMapping("/officeManage/groupManange/updateGroupMembers.do")
	@ResponseBody
	public Vo updateGroupMembers(@RequestParam("groupId")Integer groupId,@RequestParam("userIds[]")int[] userIds){
		noteService.updateGroupMembers(groupId, userIds);
		
		return Vo.succ("更新成功");
	}
	
	@RequestMapping("/officeManage/groupManange/showJoinGroups.do")
	public ModelAndView showJoinGroups(ModelAndView mav,HttpSession session){
		User currentUser = (User) session.getAttribute("user");
		if(null == currentUser){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("groupUsers", noteService.showJoinGroups(currentUser));
		
		mav.setViewName("/groupManage/showJoinGroups");
		return mav;
	}
	
	/**
	 * 任务管理
	 */
	@RequestMapping("officeManage/taskManange/showNoFinishedTasks.do")
	public ModelAndView showNoFinishedTasks(ModelAndView mav,HttpSession session){
		User user = (User) session.getAttribute("user");
		if(null == user){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("userTasks", noteService.showNoFinishedTasks(user));
		
		mav.setViewName("groupManage/taskManage/noFinishedTask");
		return mav;
	}
	
	@RequestMapping("officeManage/taskManange/showFinishedTasks.do")
	public ModelAndView showFinishedTasks(ModelAndView mav,HttpSession session){
		User user = (User) session.getAttribute("user");
		if(null == user){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("userTasks", noteService.showFinishedTasks(user));
		
		mav.setViewName("groupManage/taskManage/finishedTask");
		return mav;
	}
	
	@RequestMapping("officeManage/taskManange/taskDetail.do")
	public ModelAndView taskDetail(@RequestParam("taskId")Integer taskId,ModelAndView mav){
		Task task = taskMapper.selectTaskByTaskId(taskId);
		if(task != null){
			mav.addObject("task", task);
			mav.addObject("user", userMapper.selectUserByUserId(task.getCreateUserId()));
		}
		
		mav.setViewName("groupManage/taskManage/taskDetail");
		return mav;
	}
	
	@RequestMapping("officeManage/taskManage/finishTask.do")
	@ResponseBody
	public Vo finishTask(@RequestParam("taskId")Integer taskId,HttpSession session){
		User user = (User)session.getAttribute("user");
		noteService.finishTask(taskId,user);
		
		return Vo.succ("确认完成任务成功");
	}
	
	@RequestMapping("officeManage/group/enterGroup.do")
	public ModelAndView enterGroup(@RequestParam("groupId")Integer groupId,ModelAndView mav,HttpSession session){
		User user = (User)session.getAttribute("user");
		if(null == user){
			mav.addObject("errorMsg","请先登陆!");
			mav.setViewName("error");
			return mav;
		}
		mav.addObject("currentUser", user);
		mav.addObject("groupMembers",noteService.selectCurrentGroupMembers(groupId));
		mav.addObject("group", groupMapper.selectGroupByGroupId(groupId));
		mav.addObject("groupInforms", noteService.showGroupInforms(groupId));
		mav.addObject("groupTasks",taskMapper.selectTasksByGroupId(groupId));
		
		mav.setViewName("groupManage/group/groupDetail");
		return mav;
	}
	
	@RequestMapping("officeManage/taskManage/saveTask.do")
	@ResponseBody
	public  Vo saveTask(@RequestParam("groupId")Integer groupId,
									  @RequestParam("taskName")String taskName,
									  @RequestParam("taskContent")String taskContent,
									  @RequestParam("userIds[]")int[] userIds,
									  @RequestParam("endTime")String endTime,HttpSession session){
		User user = (User)session.getAttribute("user");
		noteService.saveTask(groupId, taskName, taskContent, userIds , endTime, user);
		
		return Vo.succ("指派任务成功");
	}
	
	/**
	 * 通知管理
	 */
	@RequestMapping("officeManage/groupManage/saveInform.do")
	@ResponseBody
	public Vo saveInform(@RequestParam("groupId")Integer groupId,
										@RequestParam("informContent")String informContent,HttpSession session){
		User user = (User)session.getAttribute("user");
		noteService.addInform(groupId, informContent, user);
		
		return Vo.succ("添加通知成功");
	}
	
	@RequestMapping("officeManage/group/deleteInform.do")
	@ResponseBody
	public Vo deleteInform(@RequestParam("informId")Integer informId){
		try{
			informMapper.deleteInformById(informId);
			return Vo.succ("删除成功");
		}catch(Exception ex){
			throw new ServiceException("删除失败", ex);
		}
	}
	
	@RequestMapping("officeManage/group/updateInform.do")
	public ModelAndView updateInform(@RequestParam("informId")Integer informId,ModelAndView mav){
		mav.addObject("inform", informMapper.selectinFormById(informId));
		
		mav.setViewName("groupManage/group/informContent");
		return mav;
	}
	
	@RequestMapping("officeManage/group/saveUpdateInform.do")
	@ResponseBody
	public Vo saveUpdateInform(@RequestParam("informId")Integer informId,@RequestParam("informContent")String informContent){
		try{
			Inform inform = new Inform();
			inform.setId(informId);
			inform.setContent(informContent);
			informMapper.updateInformById(inform);
		}catch(Exception ex){
			throw new ServiceException("更改失败",ex);
		}
		return Vo.succ("更改通知成功");
	}
	
	/**
	 * 资源文件管理
	 */
	@RequestMapping("officeManage/group/getGroupFiles.do")
	public ModelAndView getGroupFiles(@RequestParam("groupId")Integer groupId,ModelAndView mav){
		mav.addObject("groupId", groupId);
		mav.addObject("groupFiles", noteService.getGroupFiles(groupId));
		
		mav.setViewName("groupManage/group/fileUpload");
		return mav;
	}
	
	@RequestMapping("officeManage/group/downloadGroupFile/{groupId}/{fileName}")
	public ModelAndView downloadFile(@PathVariable("groupId") Integer groupId, @PathVariable("fileName") String fileName,ModelAndView mav){
		File file = noteService.downloadGroupFile(groupId, fileName);
		if(null == file){
			mav.addObject("errorMsg","文件不存在！");
			mav.setViewName("error");
			return mav;
		}
		mav.setView(new DownloadView(file));
		return mav;
	}
	
	@RequestMapping("officeManage/group/fileUpload.do")
	public ModelAndView fileUpload(@RequestParam("groupId")Integer groupId,
														 @RequestParam("groupFiles")MultipartFile[] groupFiles,HttpSession session,ModelAndView mav){
		try{
			noteService.uploadGroupFile(groupId, groupFiles);
		}catch(Exception ex){
			mav.addObject("uploadErr", "上传文件失败");
		}
		enterGroup(groupId, mav, session);
		return mav;
	}
}